package com.base.core.pager;

/**
 *
 * @Description:分页上下文
 * @author biuco
 * 2020年3月7日下午8:24:58
 *
 */
public class PageContext {

    private static final ThreadLocal<Long> currentPageHolder = new ThreadLocal<Long>();
    private static final ThreadLocal<Long> pageSizeHolder = new ThreadLocal<Long>();
    private static final ThreadLocal<Long> dataCountHolder = new ThreadLocal<Long>();

    public static Long getPageSize() {
        return pageSizeHolder.get();
    }

    public static void setPageSize(Long ps) {
        pageSizeHolder.set(ps);
    }

    public static void setCurrentPage(Long cp){
        currentPageHolder.set(cp);
    }

    public static Long getStart(){
        Long currentPage = currentPageHolder.get();
        if(currentPage == null || currentPage == 0L){
            currentPage = 1L;
        }
        if(currentPage.compareTo(1L) == -1){
            currentPage = 1L;
        }
        Long pageSize = pageSizeHolder.get();
        if(pageSize == null){
            pageSize = 10L;
        }
        return (currentPage - 1) * pageSize;
    }

    public static Long getCurrentPage(){
        Long currentPage = currentPageHolder.get();
        if(currentPage == null){
            currentPage = 1L;
        }
        return currentPage;
    }

    public static Long getDataCount(){
        return dataCountHolder.get();
    }

    public static void setDataCount(Long count){
        dataCountHolder.set(count);
    }

    public static void clear(){
        currentPageHolder.remove();
        pageSizeHolder.remove();
        dataCountHolder.remove();
    }

}
